﻿using InterestCalculationBiz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace InterestCalculation
{
    public partial class Calculators : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCalculateLoan_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime settlementDate = DateTime.Today;
                DateTime firstPaymentDate = DateTime.Today;
                firstPaymentDate.AddMonths(5);
                DataTable dt = new DataTable();
                var pair = MicroLoan.CalculateEMIWeekly(Convert.ToDouble(txtPrincipal.Text), Convert.ToInt32(txtLoanDurationWeeks.Text), Convert.ToInt32(txtInterestRate.Text), Convert.ToDateTime(txtLoanDate.Text), Convert.ToDateTime(txtFirstRepaymentDate.Text), Convert.ToInt32(ddlLoanType.SelectedItem.Value));
                double[] strRepaymentAndInterest = pair.Key;
                dt = pair.Value;
                gvLoanDetails.DataSource = dt;
                gvLoanDetails.DataBind();
                lblRepayment.Text = strRepaymentAndInterest[0].ToString();
                lblTotalInterest.Text = strRepaymentAndInterest[1].ToString();
                lblNoofRepayments.Text=txtLoanDurationWeeks.Text;
            }
            catch (Exception ex)
            {
                lblErrror.Text = ex.Message;
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            // Set Date Time value into the TextBox control
            txtFirstRepaymentDate.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");

            // Hide the Calendar control after selecting the date
            Calendar1.Visible = false;

            // Change the Button text back to "+"
            btnFirstRepaymentDate.Text = "+";
        }

        protected void btnFirstRepaymentDate_Click(object sender, EventArgs e)
        {
            // If Calendar is not visible
            // then show it and set the
            // Button text to "-"
            if (Calendar1.Visible == false)
            {
                Calendar1.Visible = true;
                btnFirstRepaymentDate.Text = "-";
            }
            else
            {
                // Hide the Calendae control
                // if it is in visible state.
                Calendar1.Visible = false;

                // set the button text back to "+"
                btnFirstRepaymentDate.Text = "+";
            }
        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            // Set Date Time value into the TextBox control
            txtLoanDate.Text = Calendar2.SelectedDate.ToString("MM/dd/yyyy");

            // Hide the Calendar control after selecting the date
            Calendar2.Visible = false;

            // Change the Button text back to "+"
            btnLoanDate.Text = "+";
        }

        protected void btnLoanDate_Click(object sender, EventArgs e)
        {
            // If Calendar is not visible
            // then show it and set the
            // Button text to "-"
            if (Calendar2.Visible == false)
            {
                Calendar2.Visible = true;
                btnLoanDate.Text = "-";
            }
            else
            {
                // Hide the Calendae control
                // if it is in visible state.
                Calendar2.Visible = false;

                // set the button text back to "+"
                btnLoanDate.Text = "+";
            }
        }

    }
}